﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments
{
    class NTE
    {
        public StringBuilder nte_id { get; set; }
        public StringBuilder Source_of_Comment { get; set; }
        public StringBuilder comment { get; set; }


        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public NTE()
        {
            nte_id = new StringBuilder();
            Source_of_Comment = new StringBuilder();
            comment = new StringBuilder();
        }

        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                 if (i + 3 == source.Length)
                        break;
               else if (check(source, i) != null)
                {
                    nte_id.Capacity = nte_id.Length;
                    Source_of_Comment.Capacity = Source_of_Comment.Length;
                    comment.Capacity = comment.Length;
                    return i - 1;
                }
              else  if (source[i] == '|')
                {
                    if (x == 3)
                    {
                        nte_id.Capacity = nte_id.Length;
                        Source_of_Comment.Capacity = Source_of_Comment.Length;
                        comment.Capacity = comment.Length;
                        return i;
                    }
                    else
                    {
                        x++;
                        continue;
                    }
                }
                else
                {
                    if (x == 1)  // passing prameter of set id - PID
                    {
                        if (source[i] != '|')
                        {
                            nte_id.Insert(nte_id.Length, source[i]);
                        }
                    }
                    else if (x == 2)
                    {
                        if (source[i] != '|')
                        {
                            Source_of_Comment.Insert(Source_of_Comment.Length, source[i]);
                        }
                    }                    
                    else if (x == 3)
                    {
                        if (source[i] != '|')
                        {
                            comment.Insert(comment.Length, source[i]);
                        }
                    }                   
                }
            }
            return i;
        }

        public override string ToString()
        {
            string nte_string = "NTE";


            if (nte_id.Length != 0)
                nte_string += "|" + nte_id;
            if (Source_of_Comment.Length != 0)
                nte_string += "|" + Source_of_Comment;
            if (comment.Length != 0)
                nte_string += "|" + comment;
           

            return nte_string;
        }
    }
}
